package demo11;

import java.util.concurrent.*;

/**
 * @author: WangPeng
 * @createTime: 2022/11/02 20:48
 * @description:
 * @version: v1.0
 */
public class Test01 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
    //第一种
        //创建一个Callable类型的任务对象
        MyCallabke myCallabke = new MyCallabke();
        //把任务返回的结果也封装到FutureTask对象中
        FutureTask task=new FutureTask(myCallabke);
        //创建一个线程对象，把上面的任务交于该线程对象。
        Thread thread = new Thread(task);
        //开启线程
        thread.start();
        //获取返回结果
        System.out.println(task.get());
    //第二种
        ExecutorService executorService1 = Executors.newFixedThreadPool(2);
        Future<Integer> future1 = executorService1.submit(new MyCallabke());
        System.out.println(future1.get());
    //第三种
        /*
          int corePoolSize, 核心线程的个数
          int maximumPoolSize, 最大线程的个数
          long keepAliveTime, 线程对象空闲的时间
          TimeUnit unit, 空闲时间单位
          BlockingQueue<Runnable> workQueue：等待队列
         */
        ThreadPoolExecutor executorService2 = new ThreadPoolExecutor(
                2,5,2000,
                TimeUnit.SECONDS,new LinkedBlockingDeque<>(3));
        Future<Integer> future2 = executorService2.submit(new MyCallabke());
        System.out.println(future2.get());

    }
}

class MyCallabke implements Callable<Integer>{
    @Override
    public Integer call() throws Exception {
        int sum = 0;
        for (int i = 0; i < 101; i++) {
            sum+=i;
        }
        return sum;
    }
}
